import { handleActions } from 'redux-actions'
import { success } from '../ui-components'

export default handleActions({
  'fetch documents': (state, { payload }) => {
    return {
      ...state,
      list: payload.documentVOs,
			fetchId: Symbol('fetch')
    }
  },

  /*
  'delete document': (state, { payload }) => {
    const { documentId } = payload
		success('删除文档成功!')
    return {
      ...state,
      list: state.list.filter(item => item.documentId != documentId)
    }
  },
  */

  /*
	'download document': (state, { payload }) => {
		const { presignedUrl } = payload
		location.href = presignedUrl
		return state
	},
  */

  /*
  'create document': (state, { payload }) => {
    return {
      ...state,
      waitUploadList: [
        ...state.waitUploadList,
        payload
      ]
    }
  },
  */

  /*
	'update document': (state, { payload }) => {
		return {
			...state,
			list: state.list.map(doc => {
				if (doc.documentId === payload.documentId) {
					return {
						...doc,
						...payload
					}
				}
				return doc
			})
		}
	},
  */

  /*
  'fetch document': (state, { payload }) => {
    const { documentId } = payload
    if (state.list.every(doc => doc.documentId != documentId)) {
			success('添加文档成功!')
      return {
        ...state,
        list: [
          ...state.list,
          payload
        ]
      }
    }
    return {
      ...state,
      list: state.list.map(doc => {
        if (doc.documentId == documentId) {
          return payload
        }
        return doc
      })
    }
  }
  */
}, {
  list: null,
  waitUploadList: []
})
